<script setup>
import { ref, computed, onMounted } from 'vue'
import {  useRoute, RouterView } from 'vue-router'
import { ElMessageBox } from 'element-plus'

import {
  HomeFilled,
  DataLine,
  Edit,
  User,
  Setting,
  SwitchButton
} from '@element-plus/icons-vue'
import { useUserStore } from '@/stores/userStores'
import { useTeacherStores } from '@/stores/teacherStores.js'
import axios from "axios";
import baseURL from "@/config/config.js";

import router from "@/router/index.js";
import {storeToRefs} from "pinia";
const route = useRoute()
const { token } = storeToRefs(useUserStore())
const { getInfoByToken, resetData } = useUserStore()
const { teacherInit, resetTeaData } = useTeacherStores()

// 当前激活的菜单项
const activeMenu = computed(() => route.path)

// 当前页面标题
const currentPageTitle = computed(() => {
  const pathMap = {
    '/teacher/home': '首页',
    '/teacher/scores-overview': '成绩总览',
    '/teacher/scores-management': '成绩管理',
    '/teacher/my-classes': '我的课程',
    '/teacher/profile': '个人信息'
  }
  return pathMap[route.path] || '首页'
})
// 处理菜单选择
const handleSelect = (index) => {
  router.push(index)
}
// 跳转到个人信息页面
const goToProfile = () => {
  router.push('/teacher/profile')

}
// 处理下拉菜单命令
const handleCommand = (command) => {
  if (command === 'profile') {
    router.push('/teacher/profile')
  } else if (command === 'logout') {
    handleLogout()
  }
}

// 处理退出登录
const handleLogout = () => {
  ElMessageBox.confirm(
      '确定要退出登录吗？',
      '提示',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }
  ).then(() => {
    const token = localStorage.getItem('token');
    localStorage.removeItem('token')
    resetData()
    resetTeaData()
    axios({
      url: baseURL + '/to-logout',
      method: 'post',
      headers: {
        Authorization: `Bearer ${token}`,
      }
    })
    router.push('/')
  })
}
// 此处获取详细信息
onMounted(() =>{
  const localToken = localStorage.getItem('token')
  if (localToken) {
    token.value = localStorage.getItem('token')
    getInfoByToken()
  }else{
    router.push('/')
  }
  axios({
    url: baseURL + '/init/teacher',
    method: 'get',
    headers: {
      Authorization: `Bearer ${localStorage.getItem('token')}`
    }
  }).then(({data}) => {
    console.log("教师初始化：", data)
    teacherInit(data.data)
  })
})

</script>

<template>
  <div class="teacher-layout">
    <!-- 侧边栏 -->
    <el-aside class="sidebar" width="250px">
      <div class="sidebar-header">
        <img src="@/assets/logo.svg" alt="Logo" class="logo">
        <h2><span>学生成绩管理系统</span><br><span>（教师端）</span></h2>
      </div>

      <!-- 主要功能菜单 -->
      <el-menu
          :default-active="activeMenu"
          class="main-menu"
          @select="handleSelect"
          :router="true"
      >
        <el-menu-item index="/teacher/index">
          <el-icon><HomeFilled /></el-icon>
          <span>首页</span>
        </el-menu-item>
        
        <el-menu-item index="/teacher/scores-overview">
          <el-icon><DataLine /></el-icon>
          <span>成绩总览</span>
        </el-menu-item>
        
        <el-menu-item index="/teacher/scores-management">
          <el-icon><Edit /></el-icon>
          <span>成绩管理</span>
        </el-menu-item>
        
        <el-menu-item index="/teacher/my-classes">
          <el-icon><User /></el-icon>
          <span>我的课程</span>
        </el-menu-item>
      </el-menu>
      
      <!-- 底部功能菜单 -->
      <div class="bottom-menu">
        <el-menu>
          <el-menu-item index="/teacher/profile" @click="goToProfile">
            <el-icon><Setting /></el-icon>
            <span>个人信息</span>
          </el-menu-item>
          
          <el-menu-item index="logout" @click="handleLogout">
            <el-icon><SwitchButton /></el-icon>
            <span>退出登录</span>
          </el-menu-item>
        </el-menu>
      </div>
    </el-aside>
    
    <!-- 主要内容区域 -->
    <el-container class="main-container">
      <el-header class="header">
        <div class="header-left">
          <el-breadcrumb>
            <el-breadcrumb-item>教师</el-breadcrumb-item>
            <el-breadcrumb-item>{{ currentPageTitle }}</el-breadcrumb-item>
          </el-breadcrumb>
        </div>
        <div class="header-right">
          <el-dropdown @command="handleCommand">

            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="profile">个人信息</el-dropdown-item>
                <el-dropdown-item command="logout" divided>退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>
      
      <el-main class="main-content">
        <RouterView></RouterView>
      </el-main>
    </el-container>
  </div>
</template>

<style scoped>
.teacher-layout {
  display: flex;
  height: 100vh;
  background-color: #f5f7fa;
  width: 100vw;
}

.sidebar {
  height: 100vh;
  background: #fff;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  display: flex;
  flex-direction: column;
}

.sidebar-header {
  padding: 20px;
  text-align: center;
  border-bottom: 1px solid #f0f0f0;
}

.logo {
  width: 48px;
  height: 48px;
  margin-bottom: 10px;
}

.sidebar-header h2 {
  margin: 0;
  font-size: 18px;
  color: #1f2937;
  font-weight: 600;
}

.main-menu {
  flex: 1;
  border-right: none;
}

.bottom-menu {
  border-top: 1px solid #f0f0f0;
}

.bottom-menu :deep(.el-menu) {
  border-right: none;
}

.main-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.header {
  background: #fff;
  border-bottom: 1px solid #f0f0f0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
}

.header-right {
  display: flex;
  align-items: center;
}

.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.username {
  margin-left: 8px;
  font-size: 14px;
  color: #1f2937;
}

.main-content {
  flex: 1;
  overflow: auto;
  padding: 20px;
  background-color: #f5f7fa;
}

:deep(.el-menu-item) {
  height: 50px;
  line-height: 50px;
}

:deep(.el-menu-item.is-active) {
  background-color: #e6f4ff;
  color: var(--el-color-primary);
}

:deep(.el-menu-item:hover) {
  background-color: #f5f7fa;
}

:deep(.el-menu-item .el-icon) {
  margin-right: 10px;
  font-size: 18px;
}
</style>